package com.nolon.flange.view

import android.R
import android.content.Context
import android.graphics.Canvas
import android.graphics.Rect
import android.graphics.drawable.Drawable
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ItemDecoration

/**
 * Created by Arpit on 23-04-2015.
 */
class DividerItemDecoration(context: Context, showtopbottomdividers: Boolean, show: Boolean) :
    ItemDecoration() {
    private val mDivider: Drawable?
    private val show: Boolean
    private var leftPaddingPx = 0
    private var rightPaddingPx = 0
    private val showtopbottomdividers: Boolean
    override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        super.onDraw(c, parent, state)
        if (!show) return
        if (mDivider != null) drawVertical(c, parent)
    }

    /**
     * Draws the divider on the canvas provided by RecyclerView Be advised - divider will be drawn
     * before the views, hence it'll be below the views of adapter
     */
    private fun drawVertical(c: Canvas, parent: RecyclerView) {
        val left = parent.paddingLeft + leftPaddingPx
        val right = parent.width - parent.paddingRight - rightPaddingPx
        val childCount = parent.childCount
        val range = if (showtopbottomdividers) 0 until childCount - 1 else 1 until childCount - 1
        for (i in range) {
            val child = parent.getChildAt(i)
//            val viewType = parent.getChildViewHolder(child).itemViewType
            // 为不同的ViewType生成不同的divider
//            if (viewType == RecyclerAdapter.TYPE_HEADER_FILES
//                    || viewType == RecyclerAdapter.TYPE_HEADER_FOLDERS) {
//                // no need to decorate header views
//                continue;
//            }
            val params = child.layoutParams as RecyclerView.LayoutParams
            val top = child.bottom + params.bottomMargin
            val bottom = top + mDivider!!.intrinsicHeight
            mDivider.setBounds(left, top, right, bottom)
            mDivider.draw(c)
        }
    }

    override fun getItemOffsets(
        outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State
    ) {
        super.getItemOffsets(outRect, view, parent, state)
        if (parent.getChildAdapterPosition(view) == 0) {

            // not to draw an offset at the top of recycler view
            return
        }
        outRect.top = mDivider!!.intrinsicHeight
    }

    companion object {
        private val ATTRS = intArrayOf(R.attr.listDivider)
    }

    init {
        val typedArray = context.obtainStyledAttributes(ATTRS)
        mDivider = typedArray.getDrawable(0)
        typedArray.recycle()
        this.show = show
        this.showtopbottomdividers = showtopbottomdividers
        leftPaddingPx = (56 * (context.resources.displayMetrics.densityDpi / 160f)).toInt()
        rightPaddingPx = (8 * (context.resources.displayMetrics.densityDpi / 160f)).toInt()
    }
}